﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net;
using Cosmetic;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
namespace WebApp.Shopping
{
    public partial class merTest : System.Web.UI.Page
    {
        private OrderManager m_orderManger = new OrderManager();
        private ProductManager m_ProductManager = new ProductManager();
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                Order theOrderes = m_orderManger.GetOrderByIds1(Request.QueryString["TradeNo"]);
                Request.ContentEncoding.GetBytes("GBK");
                Server.UrlEncode("GBK");
                string orderId = Request.QueryString["TradeNo"];
                string submitUrl = "http://61.50.136.220/gateway/cashback.jsp";
                string desKey = "A2F167FDBCBF2A0D70B0FDD5C81CF8C71991409D2C2F1CE3";
                string txCode = "TP006";
                string merchantId = "10075";
                string transDateTime = "20081217121212";
                string curCode = "156";
                string  amount = Convert .ToString( Convert.ToInt32 (theOrderes.Price + theOrderes.DeliveryPrice) *100);
                string merURL = "http://www.jj-9.com/Shopping/receive.aspx";
                string transType = "DD01";
                string info1 = "01|"+ theOrderes .DeliveryCellPhone;
                string info2 = "";
                string desAmount = ThreeDES.ThreeDES.Encrypt3DES(amount, desKey);
                string desInfo1 = ThreeDES.ThreeDES.Encrypt3DES(info1, desKey);
                StringBuilder sb = new StringBuilder();
                sb.Append("txCode=" + txCode);
                sb.Append("&merchantId=" + merchantId);
                sb.Append("&transDateTime=" + transDateTime);
                sb.Append("&orderId=" + orderId);
                sb.Append("&curCode=" + curCode);
                sb.Append("&amount=" + desAmount);
                sb.Append("&merURL=" + merURL);
                sb.Append("&transType=" + transType);
                sb.Append("&info1=" + desInfo1);
                sb.Append("&info2=" + info2);

                string str = sb.ToString();

                string mer_whtOrderNo = "";


                //装载解析密钥
                UdpaySecurity.UdpayRsa.LoadSignKey();
                string sign = UdpaySecurity.UdpayRsa.GenerateSigature(str);



                string finalStr = connectURL(submitUrl + "?" + str + "&sign=" + sign);
                string resule = finalStr.Substring(finalStr.IndexOf("success=") + 8, 1); //获取成功标志位
                if (resule.Equals("Y"))
                {
                    //如果为订单申请
                    if (transType.Equals("DD01"))
                    {
                        mer_whtOrderNo = cleraZero(finalStr.Substring(finalStr.IndexOf("returnInfo=") + 11, finalStr.IndexOf("&whtFlow=")));//去0后的网汇通订单号码                  
                        //掉用订单显示接口为用户显示汇款单填写格式
                        string showOrder = "displayRemit.html?mer_whtOrderNo=" + mer_whtOrderNo + "&mer_amount=" + amount; //+ "&mer_orderNo=商户订单号&mer_name=";
                        //调用订单显示demo
                        Response.Redirect(showOrder, true);
                    }
                    //如果为订单查询
                    if (transType.Equals("DD02"))
                    {
                        //根据需要拆分resule，获取需要的信息
                        Response.Write(resule);
                    }

                }


            }
        }

    public string connectURL(string url)
    {
        WebClient MyWebClient = new WebClient();
        MyWebClient.Credentials = CredentialCache.DefaultCredentials;
        Byte[] pageData = MyWebClient.DownloadData(url);
        string pageHtml = Encoding.GetEncoding("GBK").GetString(pageData);
        return pageHtml;
    }
    public string cleraZero(string str)
    {
        char[] tempUserId = str.ToCharArray();
        int notZero = 0;
        for (int i = 0; i < tempUserId.Length; i++)
        {
            if (tempUserId[i] != '0')
            {
                notZero = i;
                break;
            }
        }
        string shortUserId = str.Substring(notZero);
        return shortUserId;

    }
        
}



        }


